/* Thumbs.java Copyright 2010 by Jo Pol
 *
 * This file is part of fs-thumbs.
 *
 * fs-thumbs  is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * fs-thumbs  is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with BobbinWork.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.fs_thumbs;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.text.MessageFormat;

import com.googlecode.fs_thumbs.FsCollection.FsException;
import com.googlecode.fs_thumbs.FsCollection.Waypoint;

public class MultiPage extends Main {

	private final String outputDir;

	protected MultiPage(String[] args, FsCollection fsCollection, String outputDir) //
			throws FileNotFoundException, IOException, FsException, FsException, FsException {
		super(args);
		this.outputDir = outputDir;
		final PrintStream stream = new PrintStream(outputDir + "/index.html");
		stream.println(String.format(mainHead, htmlTitle(fsCollection)));
		printMuiltPageNode(fsCollection, stream);
		stream.println(MessageFormat.format(mainFoot, count));
		stream.close();
	}

	private void printMuiltPageNode(final FsCollection fsCollection, final PrintStream mainStream) //
			throws FsException, FsException, IOException, FsException {

		for (final Waypoint waypoint : fsCollection.getChildReferences()) {
			FsCollection child = fsCollection.getCollection(waypoint.getId());
			if (waypoint.isLeaf()) {
				final String fileName = waypoint.getId() + ".html";
				final PrintStream subStream = new PrintStream(outputDir + "/" + fileName);
				subStream.println(String.format(detailHead, htmlTitle(child)));
				final FsCollection leafCollection = printLeaf(child, subStream);
				subStream.println(detailFoot);
				subStream.close();
				mainStream.print(crumbsAsHtmlHeaders(leafCollection));
				mainStream.println(MessageFormat.format(linkToDetailsPage, fileName, leafCollection.getChildCount()));
			} else {
				printMuiltPageNode(child, mainStream);
			}
		}
	}
}
